<%@ page language="java" contentType="text/html; charset=utf-8"
	pageEncoding="utf-8"%>
<%@ taglib prefix="c" uri="http://java.sun.com/jsp/jstl/core"%>
<%@ taglib prefix="fn" uri ="http://java.sun.com/jsp/jstl/functions"%>
<div class="page-header">
	<h3>添加用户</h3>
</div>
<form role="form">
	<div class="form-group">
		<label for="username">用户名</label><input name="username" type="text"
			class="form-control" id="username" /> <br /> <label for="email">邮箱地址</label><input
			name="email" type="email" class="form-control" id="email" /> <br />
		<label for="password1">密码</label><input name="password"
			type="password" class="form-control" id="password1" /> <br /> <label
			for="password2">重复密码</label><input type="password"
			class="form-control" id="password2" /><br /> <label>角色</label><select
			name="role" class="form-control">
			<option>admin</option>
			<option>manager</option>
		</select><br /> <label>分配产品</label>
		<div>
			<c:forEach var="product" items="${result.object}">
				<label class="checkbox-inline"> <input name="productIds"
					type="checkbox" value="${product.id}">${fn:escapeXml(product.name)}
				</label>
			</c:forEach>
		</div>
	</div>
	<input id="addUser" type="submit" value="提交" class="btn btn-default" />
</form>
<script>
	$(function() {
		$("input#addUser").click(
				function(e) {
					var username = $("input[name=username]").val();
					var email = $("input[name=email]").val();
					var password = $("input[name=password]").val();
					var password2 = $("input#password2").val();
					var role = $("select[name=role]").val();
					var productIds = [];
					$("input[name=productIds]:checked").each(function() {
						productIds.push($(this).val());
					});
					if (username == null || username == "") {
						layer.tips('用户名不能为空', 'input[name=username]', {
							tips : 3
						});
					} else if (email == null || email == "") {
						layer.tips('邮箱不能为空', 'input[name=email]', {
							tips : 3
						});
					} else if (password == null || password == "") {
						layer.tips('密码不能为空', 'input[name=password]', {
							tips : 3
						});
					} else if (password2 == null || password2 == "") {
						layer.tips('重复密码不能为空', 'input#password2', {
							tips : 3
						});
					} else if (password != password2) {
						layer.tips('两次输入密码不一致', 'input[name=password]', {
							tips : 3
						});
					} else {
						$.ajax({
							type : 'post',
							traditional : true,
							url : '${basePath}/user/add',
							data : {
								username : username,
								email : email,
								password : password,
								role : role,
								productIds : productIds
							},
							success : function(data) {
								if (data.result.success) {
									layer.msg(data.result.message, {
										icon : 1,
										time : 1000
									}, function() {
										$("#rightDiv").load(
												"${basePath}/user/add");
									});
								} else if (data.result.message == "用户名已被使用") {
									layer.tips(data.result.message,
											'input[name=username]', {
												tips : 3
											});
								} else {
									layer.msg(data.result.message, {
										icon : 2
									});
								}
							}
						});
					}
					return false;
				});
	});
</script>